package school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import school.domain.Bill;
import school.mapper.BillMapper;
import school.support.BaseController;
import school.support.pojo.BillPo;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

@RequestMapping("bill")
@Controller
public class BillController extends BaseController {

    @Autowired
    private BillMapper billMapper;

    /**
     * 列表   cha kan
     *
     * @param model
     * @param pageNum
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "") String username,
                       @RequestParam(defaultValue = "") String phone,
                       @RequestParam(defaultValue = "") String tcname,
                       String starttime,String endtime) {
        Example example = new Example(Bill.class);
        Example.Criteria criteria = example.createCriteria()
                .andCondition("u.name like ", "%" + username + "%")
                .andCondition("u.phone like ", "%" + phone + "%")
                .andCondition("t.name like ", "%" + tcname + "%");

        if(!StringUtil.isEmpty(starttime)){
            criteria.andCondition("b.addtime >= ",starttime);
        }
        if(!StringUtil.isEmpty(endtime)){
            criteria.andCondition("b.addtime <= ",endtime);
        }

        if (!sessionUser().getPhone().equals("admin")) {
            criteria.andCondition("u.id = ", sessionUser().getId());
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<BillPo> pageInfo = new PageInfo<BillPo>(billMapper.selectPoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "bill/list";
    }


}
